package com.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.dao.ElcInfotypeDao;
import com.ssm.entity.ElcComment;
import com.ssm.entity.ElcInfotype;

import com.ssm.entity.SysUser;
import com.ssm.interceptor.BlackFilter;
import com.ssm.service.ElcInfotypeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * (ElcInfotype)栏目管理
 *
 * @author makejava
 * @since 2021-06-01 10:37:32
 */
@RestController
@RequestMapping("elcInfotype")
public class ElcInfotypeController {

    //    引用
    @Resource
    private BlackFilter blackFilter;



    /**
     * 服务对象
     */
    @Resource
    private ElcInfotypeService elcInfotypeService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("selectOne/{id}")
    public ElcInfotype selectOne(@PathVariable Integer id) {
        return this.elcInfotypeService.queryById(id);
    }


    /**
     * 查找所有的栏目信息
     * @param elcInfotype
     * @param rows
     * @param page
     * @return
     */
    @RequestMapping("listcolumn")
    public PageInfo<ElcInfotype> queryAll(ElcInfotype elcInfotype, Integer rows, Integer page, HttpServletRequest req){

        //分页组件
        PageHelper.startPage(page,rows);
        List<ElcInfotype> list=elcInfotypeService.queryAll(elcInfotype);
        PageInfo<ElcInfotype> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 给导出表格提供数据
     * @param elcInfotype
     * @return
     */
    @RequestMapping("queryAll")
    public List<ElcInfotype> queryExcel(ElcInfotype elcInfotype){
        List<ElcInfotype> list=elcInfotypeService.queryAll(elcInfotype);
        return list;
    }

    /**
     * 添加栏目
     * @param elcInfotype
     * @return
     */



    @RequestMapping("addColumn")
    public String addColumn(ElcInfotype elcInfotype){

      if (checkColumn(elcInfotype.getTypeName()).equals("null")){
          return "null";
      }
      if (checkColumn(elcInfotype.getTypeName()).equals("false")){
          return "like";
      }


        elcInfotype.setCreateTime(new Date());
        elcInfotype.setUpdateTime(new Date());
        elcInfotypeService.insert(elcInfotype);
        return  "success";
    }



    @RequestMapping("checkColumn")
    public String  checkColumn(@RequestParam(defaultValue = "") String typeName){
        if (typeName==""){
            return "null";
        }
        return elcInfotypeService.queryByTypename(typeName)+"";
    }


    /**
     * 通过ID删除
     * @param id
     * @return
     */
    @RequestMapping("deleteById/{id}")
    public String deleteColumn(@PathVariable Integer id) {
        elcInfotypeService.deleteById(id);
        return  "success";
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("deletes/{ids}")
    public  String deletes(@PathVariable  Integer[] ids){
        elcInfotypeService.deleteByIds(ids);
        return  "success";
    }


    /**
     * 更新栏目
     * @param elcInfotype
     * @return
     */
    @RequestMapping("update")
    public String update(ElcInfotype elcInfotype){
        String name=elcInfotypeService.queryById(elcInfotype.getId()).getTypeName();

        if (checkColumn(elcInfotype.getTypeName()).equals("null")){
            return "null";
        }
        if (elcInfotype.getTypeName()!=null){
            if (!elcInfotype.getTypeName().equals(name) && checkColumn(elcInfotype.getTypeName()).equals("false")  ){
                return "like";
            }
            elcInfotype.setTypeName(blackFilter.filterWorld(elcInfotype.getTypeName()));
        }



        elcInfotype.setUpdateTime(new Date());
        elcInfotypeService.update(elcInfotype);
        return "success";
    }

}
